#include "ai.h"
#include "gameengine.h"

AI::AI()
{
}

void AI::setView(IChessView *view, Board *board , GameEngine * game)
{
    myView = view;
    myBoard = board;
    myGame = game;
}

void AI::doRandomComputerMove(){
    if(ColorCanAttack(white) && !myGame->clicked == false && myGame->whiteIsComputer){
        myView->WriteMessageArea("\nwhite could attack" +
                                 MyFunctions::convertInt(whiteTotalKillMoves) + "cells\n");
    }
    if(ColorCanAttack(black) && !myGame->clicked == false && myGame->blackIsComputer){
        myView->WriteMessageArea("black could attack" +
                                 MyFunctions::convertInt(blackTotalKillMoves) + "cells\n");
    }
    //myView->WriteMessageArea("random white move.\n");
    if(!myGame->gameStarted)
        return;
    myGame->moving = true;
    Color opp = (myGame->turn == white) ? black : white;
    if(myGame->clicked==false) {
        int test = 0;
        while(1) {
            ChessPiece * p;
            do {
                int pieceNum = rand() % myBoard->piecesOnBoard.size();
                p = myBoard->piecesOnBoard[pieceNum];
            } while(p->pColor!=myGame->turn);
            std::vector<Cell> compMoves;
            myGame->FindMoves(compMoves,p);

            if(cellsHasColor(opp,compMoves)){
                myGame->cellSelectComp(p->row,p->col);
                break;
            } else if(test > 50 && compMoves.size()!=0) {
                myGame->cellSelectComp(p->row,p->col);
                break;
            }
            test++;
        }
        myGame->clicked = true;
    } else {
        int moveNum = rand() % myGame->possibleMoves.size();
        Cell moveToDo = myGame->possibleMoves[moveNum];
        if(cellsHasColor(opp,myGame->possibleMoves)){
            std::vector<Cell> killMoves = whichCellsHasColor(opp,myGame->possibleMoves);
            moveToDo = findBestKill(killMoves);
        }
        myGame->cellSelectComp(moveToDo.row,moveToDo.col);
        myGame->clicked = false;
        myGame->moving = false;
        if(ColorCanAttack(white) && myGame->whiteIsComputer){
            myView->WriteMessageArea("white can now attack" +
                                     MyFunctions::convertInt(whiteTotalKillMoves) + "cells\n");
        }
        if(ColorCanAttack(black) && myGame->blackIsComputer){
            myView->WriteMessageArea("black can now attack" +
                                     MyFunctions::convertInt(blackTotalKillMoves) + "cells\n");
        }
    }
}


Cell AI::findBestKill(std::vector<Cell> killMoves){
    while(killMoves.size()>=2){
        Cell first = killMoves[killMoves.size()-1];
        Cell second = killMoves[killMoves.size()-2];
        if(PieceValue(first) >= PieceValue(second)){
            killMoves.pop_back();
            killMoves.pop_back();
            killMoves.push_back(first);
        } else {
            killMoves.pop_back();
            killMoves.pop_back();
            killMoves.push_back(second);
        }

    }
    return killMoves.back();
}

int AI::PieceValue(Cell c){
    int answer = 0;
    ChessPiece * p = myBoard->getPieceAt(c.row,c.col);
    answer = p->getValue();
    return answer;
}

bool AI::ColorCanAttack(Color cor){
    whiteTotalKillMoves = (cor == white) ? 0 : whiteTotalKillMoves;
    blackTotalKillMoves = (cor == black) ? 0 : blackTotalKillMoves;
    Color opp = (cor == white) ? black : white;
    std::vector<ChessPiece*>::iterator it;
    for ( it = myBoard->piecesOnBoard.begin() ; it < myBoard->piecesOnBoard.end() ; it++){
        ChessPiece * p = *it;
        if(p->pColor == cor){
            std::vector<Cell> compMoves;
            myGame->FindMoves(compMoves,p);
            int totes = cellsHasColor(opp,compMoves);
            if(totes){
                whiteTotalKillMoves = (cor == white) ?
                            whiteTotalKillMoves + totes : whiteTotalKillMoves;
                blackTotalKillMoves = (cor == black) ?
                            blackTotalKillMoves + totes : blackTotalKillMoves;
            }
        }
    }
    bool answer = (cor == white) ? whiteTotalKillMoves != 0 : blackTotalKillMoves != 0;
    return answer;
}

int AI::cellsHasColor(Color cor , std::vector<Cell> cells){
    std::vector<Cell>::iterator it;
    int answer = 0;
    for ( it = cells.begin() ; it < cells.end() ; it++){
        Cell c = *it;
        if(myBoard->getPieceAt(c.row,c.col)!=NULL){
            ChessPiece * p = myBoard->getPieceAt(c.row,c.col);
            if(p->pColor == cor){
                answer++;
            }
        }
    }
    return answer;
}

std::vector<Cell> AI::whichCellsHasColor(Color cor , std::vector<Cell> cells){
    std::vector<Cell>::iterator it;
    std::vector<Cell> answer;
    for ( it = cells.begin() ; it < cells.end() ; it++){
        Cell c = *it;
        if(myBoard->getPieceAt(c.row,c.col)!=NULL){
            ChessPiece * p = myBoard->getPieceAt(c.row,c.col);
            if(p->pColor == cor){
                answer.push_back(c);
            }
        }
    }
    return answer;
}

